package io.renren.modules.wx.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import io.renren.modules.wx.entity.TeacherEntity;
import io.renren.modules.wx.service.TeacherService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.wx.entity.CourseEntity;
import io.renren.modules.wx.service.CourseService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 *
 *
 * @author linmingze
 * @email ${email}
 * @date 2022-10-14 19:06:40
 */
@RestController
@RequestMapping("wx/course")
public class CourseController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private TeacherService teacherService;

    public static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 列表
     */
    @RequestMapping("/courseAndTeacherList")
    public R courseAndTeacherList(@RequestParam Map<String, Object> params){
        List<CourseEntity> data = courseService.courseList();

        return R.ok().put("data", data);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = courseService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 讲师列表
     */
    @RequestMapping("/tlist")
    public R TList(@RequestParam Map<String,Object> params){
        List<CourseEntity> data = courseService.courseByCondition(params);
        return R.ok().put("data",data);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{courseId}")
    public R info(@PathVariable("courseId") Integer courseId){
		CourseEntity course = courseService.getById(courseId);
        TeacherEntity teacherEntity = teacherService.getById(course.getTeacherId());
        course.setTeacherName(teacherEntity.getUsername());
        return R.ok().put("course", course);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CourseEntity course){
        course.setAddTime(new Date());
		courseService.save(course);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CourseEntity course){
		courseService.updateById(course);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] courseIds){
		courseService.removeByIds(Arrays.asList(courseIds));

        return R.ok();
    }

}
